package com.ruoyi.monitor.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.monitor.service.IPolluantAvgService;
import com.ruoyi.monitor.utils.Common.Constant;
import com.ruoyi.monitor.utils.Common.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.monitor.domain.PollutantAvg;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 均值查询Controller
 *
 * @author xzx
 * @date 2023-02-10
 */
@RestController
@RequestMapping("/monitor/avg/pollutant")
public class PollutantAvgController extends BaseController
{
    @Autowired
    private IPolluantAvgService pollutantAvgService;

    /**
     * 查询均值查询列表
     */
    @PreAuthorize("@ss.hasPermi('avg:pollutant:list')")
    @GetMapping("/list")
    public TableDataInfo list(PollutantAvg pollutantAvg)
    {
        startPage();
        List<PollutantAvg> list = pollutantAvgService.selectPollutantAvgList(pollutantAvg);
        return getDataTable(list);
    }

    /**
     * 导出均值查询列表
     */
    @PreAuthorize("@ss.hasPermi('avg:pollutant:export')")
    @Log(title = "均值查询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PollutantAvg pollutantAvg)
    {
        List<PollutantAvg> list = pollutantAvgService.selectPollutantAvgList(pollutantAvg);
        ExcelUtil<PollutantAvg> util = new ExcelUtil<PollutantAvg>(PollutantAvg.class);
        util.exportExcel(response, list, "均值查询数据");
    }

    /**
     * 获取均值查询详细信息
     */
    @PreAuthorize("@ss.hasPermi('avg:pollutant:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pollutantAvgService.getById(id));
    }

    /**
     * 新增均值查询
     */
    @PreAuthorize("@ss.hasPermi('avg:pollutant:add')")
    @Log(title = "均值查询", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PollutantAvg pollutantAvg)
    {
        return toAjax(pollutantAvgService.save(pollutantAvg));
    }

    /**
     * 修改均值查询
     */
    @PreAuthorize("@ss.hasPermi('avg:pollutant:edit')")
    @Log(title = "均值查询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PollutantAvg pollutantAvg)
    {
        Integer type = pollutantAvg.getType();
        if (Constant.DAILY.equals(type)){
            pollutantAvg.setStart(DateUtils.get0MIll(DateUtil.beginOfDay(pollutantAvg.getStart())));
            pollutantAvg.setEnd(DateUtils.get0MIll(DateUtil.endOfDay(pollutantAvg.getEnd())));
        }else if (Constant.WEEKLY.equals(type)){
            pollutantAvg.setStart(DateUtils.get0MIll(DateUtil.beginOfWeek(pollutantAvg.getStart())));
            pollutantAvg.setEnd(DateUtils.get0MIll(DateUtil.endOfWeek(pollutantAvg.getEnd())));
        }else if (Constant.MONTHLY.equals(type)){
            pollutantAvg.setStart(DateUtils.get0MIll(DateUtil.beginOfMonth(pollutantAvg.getStart())));
            pollutantAvg.setEnd(DateUtils.get0MIll(DateUtil.endOfMonth(pollutantAvg.getEnd())));
        }else if (Constant.YEARLY.equals(type)){
            pollutantAvg.setStart(DateUtils.get0MIll(DateUtil.beginOfYear(pollutantAvg.getStart())));
            pollutantAvg.setEnd(DateUtils.get0MIll(DateUtil.endOfYear(pollutantAvg.getEnd())));
        }

        return toAjax(pollutantAvgService.saveOrUpdate(pollutantAvg));
    }

    /**
     * 删除均值查询
     */
    @PreAuthorize("@ss.hasPermi('avg:pollutant:remove')")
    @Log(title = "均值查询", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pollutantAvgService.removeByIds(Arrays.asList(ids)));
    }
}
